// subPages/mine/relation/relation.js
const app = getApp()
var json = require('../../../data/json');
var util =  require('../../../utils/util.js');
var mainFlag = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeList: json.typeList,
    flag : false,
    codeDis : false,
    phoneCode : "获取验证码",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(app.globalData.userId){
      this.setData({
        userId: app.globalData.userId
      })
    }else{
      app.userIdCallback = userId => {
        if (userId != '') {
          this.setData({
            userId: userId
          });
        }
      }
    }
  },

  //获取电话
  phoneInput:function(e){
    let value = e.detail.value
    this.setData({
        phone : value
    })
  },

  //获取验证码
  smsBtn: function (options) {
    var that = this
    var phone = that.data.phone
    if(that.validationPhone(phone))
    {
      that.setData({
        codeDis : true
      })
      wx.request({
        url: app.globalData.apiUrl +'/Api/Alidayu/alyzm',
        data: {
          mobile: phone
        },
        method: 'get',
        header: {
          'content-type': 'application/json' //默认值
        },
        success: function (res) {
          // console.log(res)
          if(res.data.code != "ok"){
            that.setData({
              codeDis : false
            })
            util.showToast('获取验证码失败，请重新获取','loading',2000)
          }
          else{
            that.setData({
              phoneCode : 60,
              code: res.data.log
            })
            let time = setInterval(()=>{
              var phoneCode = that.data.phoneCode
              phoneCode --
              that.setData({
                  phoneCode : phoneCode
              })
              if(phoneCode == 0){
                clearInterval(time)
                that.setData({
                  phoneCode : "获取验证码",
                  flag : true,
                  codeDis : false
                })
              }
            },1000)
          }
        },
        fail: function (res) {
          console.log("失败");
        }
      })
    }
  },

  //验证手机号
  validationPhone:function(phone){
    if(!phone)
    {
      util.showModal('提示','手机号不能为空')      
      return false
    }
    if (phone.length === 11) {
      var type = 'phone'
      var validation = util.validation(phone,type)
      if(validation){
        return true
      }
      else{
        util.showModal('提示','手机号输入不正确')
        return false
      }
    }else{
      util.showModal('提示','手机号输入不正确')
      return false
    }
  },

  //选择状态
  typeBtn:function(e){
    var that = this
    var type = e.currentTarget.dataset.type;
    var typeList = that.data.typeList;
    for(var i in typeList)
    {
      if(type == typeList[i].type){
        typeList[i].check = true 
      }
      else
      {
        typeList[i].check = false 
      }
    }
    that.setData({
      typeList: typeList
    })
  },

  formSubmit:function(e){
    // console.log(e)
    console.log(mainFlag)
    if(mainFlag){
      return
    }
    mainFlag = true
    if(app.globalData.userId){
      var relation = e.detail.value.relation
      var name = e.detail.value.name
      var sms =  e.detail.value.sms;
      if(!relation){
        util.showModal('提示','请输入您和对方的关系')
        mainFlag = false
        return
      }
      if(name != '')
      {
        var type= 'name'
        var validation = util.validation(name,type)
        if(!validation){
          util.showModal('提示','对方姓名输入不正确')
          mainFlag = false
          return
        }
      }else{
        util.showModal('提示','请输入对方姓名')
        mainFlag = false
        return
      }
      if(sms != ''){
        if(this.data.code!= '')
        {
          if(sms != this.data.code)
          {
            util.showModal('提示','短信验证码输入不正确，请重新输入')
            mainFlag = false
            return
          }
        }
        else{
          util.showModal('提示','请获取短信验证码')
          mainFlag = false
          return
        }
      }
      else{
        util.showModal('提示','短信验证码不能为空')      
        mainFlag = false
        return
      }
      this.bindBtn(relation,name)
    }
    else{
      mainFlag = false
      util.showModelAll('提示','该功能需要授权登录后使用，请进行授权登录',function(){
        if (app.globalData.userInfo) {
          //已授权
          wx.redirectTo({
            url: '/pages/login/login'
          })
        }else{
          // 在没有 open-type=getUserInfo 版本的兼容处理
          if(app.globalData.isAuthorized){
            app.userInfoReadyCallback = res => {
              if (res) {
                //已授权
                wx.redirectTo({
                  url: '/pages/login/login'
                })
              }else{
                //未授权
                wx.navigateTo({
                  url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                })
              }
            }
          }else{
            wx.navigateTo({
              url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
            })
          }
        }
      })
    }    
    
  },

  //立即绑定
  bindBtn:function(relation,name){
    var that = this
    var typeList = that.data.typeList
    var type = ''
    for(let i in typeList){
      if(typeList[i].check)
      {
        type = typeList[i].type
        break
      }
    }
    wx.request({
      url: app.globalData.apiUrl + '/bd_father',
      data: {
        user_id: that.data.userId,
        type: type,
        gx: relation,
        name: name,
        phone: that.data.phone
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded' //默认值
      },
      success: function (res) {
        console.log(res)
        if(res.data == '1'){
          util.showToast('绑定成功','success',2000)
        }else if(res.data == '2'){
          util.showModal('提示','绑定失败，请重新绑定')
        }else if(res.data == '0'){
          util.showModal('提示','绑定失效，该用户不存在')
        }else if(res.data == '3'){
          util.showModal('提示','该关系已存在，请勿重复提交')
        }
        that.setData({
          name: '',
          relation:'',
          phone: '',
          sms: ''
        })
        mainFlag = false
      },
      fail: function (res) {
        mainFlag = false
        util.showModal('提示','绑定失败，请重新绑定')
      }
    })
  },

  //当前亲属
  currentFamily:function(){
    wx.navigateTo({
      url: '../currentFamily/currentFamily',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})